﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dia2Lib;

namespace Kokomo.Mirror.PeFormat.Symbols
{
	class TableEnumerator : SymbolBase, IEnumerator<Table>
	{
		public TableEnumerator(
			IDiaEnumTables enumTables,
			SymbolSource source
			)
			: base(source)
		{
			if (enumTables == null) throw new ArgumentNullException("enumTables");
			this._enumTables = new ComWrapper<IDiaEnumTables>(enumTables);
		}

		private ComWrapper<IDiaEnumTables> _enumTables;
		private IDiaEnumTables EnumUnknown { get { return this._enumTables.Object; } }

		private Table _current;
		public Table Current
		{
			get { return this._current; }
		}

		public void Dispose()
		{
		}

		object System.Collections.IEnumerator.Current
		{
			get { return this._current; }
		}

		public bool MoveNext()
		{
			IDiaTable diaTable = null;
			uint fetched = 0;
			this.EnumUnknown.Next(1, ref diaTable, ref fetched);
			if (fetched != 1)
			{
				this._current = null;
				return false;
			}

			this._current = this.CreateObject(diaTable);
			return true;
		}

		protected Table CreateObject(IDiaTable comObject)
		{
			return Table.FromDiaTable(comObject, this.Source);
		}

		public void Reset()
		{
			this.EnumUnknown.Reset();
		}
	}
}
